package jp.sourceforge.akjrcp.text_douga.video;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import jp.sourceforge.akjrcp.text_douga.AudioData;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.SystemEnvironment;
import jp.sourceforge.akjrcp.text_douga.VideoData;
import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.VideoWriter;
import jp.sourceforge.akjrcp.text_douga.effect.VideoEffect;

public class ColorVideoData extends EffectableVideoData{
	
	private double duration=3.0;
	//private double loopPadTime;//loop only
	//private int loop;
	//private boolean canStopInLoop=true;//don't use
	private AudioData audioData;
	//private int padColor;
	
	private VideoEffect startEffect;
	private VideoEffect endEffect;

	private int bgColor=0;
	//TODO align

	public void setStartEffect(VideoEffect startEffect) {
		this.startEffect = startEffect;
	}
	public String toString(){
	return "ColorVideoData:duration="+duration+" bgColor="+bgColor;	
	}
	public ColorVideoData(int bgColor){
		this.bgColor=bgColor;
	}

	public double getDuration() {
		return duration;
	}
	public void setDuration(double duration) {
		this.duration = duration;
	}


	public int writeData(File dir,DougaPreference preference,int startIndex,int maxframe) throws IOException {
		int wf=0;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
			//TODO cache
			BufferedImage frameImage=new BufferedImage(preference.getWidth(),preference.getHeight(),BufferedImage.TYPE_INT_RGB);//JPEG must RGB?
			Graphics2D g2=(Graphics2D) frameImage.getGraphics();
			g2.setColor(new Color(bgColor));
			g2.fillRect(0, 0, preference.getWidth(), preference.getHeight());
			int frames=0;
			if(duration==DougaSystem.FOREEVER){
			frames=maxframe;//draw all
			}else{
			frames= new BigDecimal(preference.getFps()).multiply(new BigDecimal(duration)).intValue();
			if(maxframe!=0 && maxframe<frames){
				frames=maxframe;
			}
			}
			wf=TextDougaVideoUtils.copyImage(frameImage, dir, startIndex, frames);
			
		
		//sync audio
		if(audioData!=null){
			audioData.setSynchLength(wf/preference.getFps());
		}
		
		
		//TODO if video is too short ,bad things will happen.
		if(wf>0){
			
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}
	public AudioData getAudioData() {
		return audioData;
	}
	public void setAudioData(AudioData audioData) {
		this.audioData = audioData;
	}
	public int getBgColor() {
		return bgColor;
	}
	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}
	public double getTotalTime() {
		
		return duration;
	}
	public int writeData(VideoWriter writer, DougaPreference preference) throws IOException {
		int maxframe=Math.max(0, writer.getMaxFrame()-writer.getWrited());
		int startIndex=writer.getWrited()+1;
		
		log.info("start writeData:"+"maxframe="+maxframe+" startIndex="+startIndex);
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		int wf=0;
		
		//TODO cache
		BufferedImage frameImage=new BufferedImage(preference.getWidth(),preference.getHeight(),BufferedImage.TYPE_INT_RGB);//JPEG must RGB?
		Graphics2D g2=(Graphics2D) frameImage.getGraphics();
		g2.setColor(new Color(bgColor));
		g2.fillRect(0, 0, preference.getWidth(), preference.getHeight());
		int frames=0;
		if(duration==DougaSystem.FOREEVER){
		frames=maxframe;//draw all
		}else{
			frames=new BigDecimal(preference.getFps()).multiply(new BigDecimal(duration)).intValue();
		if(maxframe!=0 && maxframe<frames){
			frames=maxframe;
			}
		}
		log.info("copy image:"+frames+" it takes frames/60 seconds to finish copy");
		
		wf=TextDougaVideoUtils.copyImage(frameImage, this,writer, frames);
		
	
	//sync audio
	if(audioData!=null){
		audioData.setSynchLength(wf/preference.getFps());
	}
	
	
//	TODO if video is too short ,bad things will happen.
	if(wf>0){
		
		if(endEffect!=null){
			endEffect.setKeyFrame(startIndex+wf-1);
		}
	}else{//0 writed
		if(startEffect!=null){
			startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
		}
	}
	
	return wf;
	}


}
